package com.edu.whty.service.impl;


import com.edu.whty.common.constants.Constant;
import com.edu.whty.common.tools.UUIDUtils;
import com.edu.whty.dao.*;
import com.edu.whty.entity.base.AttachFile;
import com.edu.whty.entity.capacity.Capacity;
import com.edu.whty.entity.disk.DiskOwner;
import com.edu.whty.entity.disk.FolderDTO;
import com.edu.whty.entity.disk.ResOwner;
import com.edu.whty.service.DiskCapacityService;
import com.edu.whty.service.DiskService;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @auther xiongxiaofei
 * @date 2016/11/14
 */
@Component("DiskCapacityService")
public class DiskCapacityServiceImpl implements DiskCapacityService {

	private Logger LOGGER = LogManager.getLogger(DiskCapacityServiceImpl.class);

	@Resource
	private DiskCapacityDao diskCapacityDao;



	/**
	 * 查询列表
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Capacity> selectList(Capacity model) throws Exception {
		return diskCapacityDao.selectList(model);
	}

	/**
	 * 查询单条记录
	 * @param capacityId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Capacity selectOne(String capacityId) throws Exception{
		return diskCapacityDao.selectOne(capacityId);
	}

	/**
	 * 更新
	 * @param model
	 * @throws Exception
	 */
	@Override
	public void updateOne(Capacity model) throws Exception{
		diskCapacityDao.updateOne(model);
	}

	/**
	 * 批量更新
	 * @param list
	 * @throws Exception
	 */
	@Override
	public void updateBatch(List<Capacity> list) throws Exception{
		diskCapacityDao.updateBatch(list);
	}

	/**
	 * 插入
	 * @param model
	 * @throws Exception
	 */
	@Override
	public void insertOne(Capacity model) throws Exception{
		diskCapacityDao.insertOne(model);
	}

	/**
	 * 批量插入
	 * @param list
	 * @throws Exception
	 */
	@Override
	public void insertBatch(List<Capacity> list) throws Exception{
		diskCapacityDao.insertBatch(list);
	}

	/**
	 * 删除
	 * @param capacityId
	 * @throws Exception
	 */
	@Override
	public void deleteOne(String capacityId) throws Exception{
		diskCapacityDao.deleteOne(capacityId);
	}

	/**
	 * 更新网盘使用量
	 * @param ownerId
	 * @throws Exception
	 */
	@Override
	public void updateUsed(String ownerId) throws Exception{
		diskCapacityDao.updateUsed(ownerId);
	}

	/**
	 * 更新网盘使用量
	 * @param list
	 * @throws Exception
	 */
	@Override
	public void updateUsedBatch(List<String> list) throws Exception{
		diskCapacityDao.updateUsedBatch(list);
	}

	/**
	 * 更新机构网盘使用量
	 * @param orgId
	 * @throws Exception
	 */
	@Override
	public void updateOrgUsed(String orgId) throws Exception{
		diskCapacityDao.updateOrgUsed(orgId);
	}

	@Override
	public Long selectTeachersCapacity(String orgId) throws Exception{
		return diskCapacityDao.selectTeachersCapacity(orgId);
	}

	@Override
	public void updateDiskCapacity(Capacity capacity, Long fileLength, String updateType)throws Exception {
		Long used = 0l;
		//updateType为1时减少网盘容量
		 if ("1".equals(updateType)) {
			 used = capacity.getUsedCapacity()-fileLength;
			 capacity.setUsedCapacity(used);
		}else{
			 used = capacity.getUsedCapacity() + fileLength;
			 capacity.setUsedCapacity(used);
		 }
		DecimalFormat format = new DecimalFormat("#.0000");
		String rate = format.format((used) / (float) capacity.getMaxCapacity()*100);
		capacity.setUsedRate(Float.parseFloat(rate));
		diskCapacityDao.updateOne(capacity);
	}


}
